package com.lzmpt.imchat.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lzmpt.imchat.R;

public class ChatVedioTypePoPu extends PopupWindow implements View.OnClickListener {

    private View mPopView;
    private Activity context;

    private LinearLayout mLlItem1, mLlItem2;
    private ImageView mImgItem1, mImgItem2;
    private TextView mTvItem1, mTvItem2, mTvcancel;


    public ChatVedioTypePoPu(Activity context) {
        super(context);
        this.context = context;
        init(context);
        setPopupWindow();
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        mPopView = inflater.inflate(R.layout.popup_chat_vedio_type, null);

        mLlItem1 = mPopView.findViewById(R.id.ll_popumenu_1);
        mLlItem2 = mPopView.findViewById(R.id.ll_popumenu_2);

        mImgItem1 = mPopView.findViewById(R.id.img_popumenu_1);
        mImgItem2 = mPopView.findViewById(R.id.img_popumenu_2);

        mTvItem1 = mPopView.findViewById(R.id.tv_popumenu_1);
        mTvItem2 = mPopView.findViewById(R.id.tv_popumenu_2);

        mTvcancel = mPopView.findViewById(R.id.tv_popumenu_3);

        mLlItem1.setOnClickListener(this);
        mLlItem2.setOnClickListener(this);
        mTvcancel.setOnClickListener(this);
    }

    public void setMenu(String title1, String title2, String title3, int icon1, int icon2) {
        setMenu(title1, title2, title3);
        setMenuIcon(icon1, icon2);
    }

    public void setMenu(String title1, String title2, String title3) {
        setMenuIcon(0, 0);
        if (!TextUtils.isEmpty(title1)) {
            mTvItem1.setText(title1);
            mLlItem1.setVisibility(View.VISIBLE);
        } else {
            mLlItem1.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(title2)) {
            mTvItem2.setText(title2);
            mLlItem2.setVisibility(View.VISIBLE);
        } else {
            mLlItem2.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(title3)) {
            mTvcancel.setText(title3);
        } else {
            mTvcancel.setText("取消");
        }

    }

    public void setMenuIcon(int icon1, int icon2) {
        if (icon1 != 0) {
            mImgItem1.setImageResource(icon1);
            mImgItem1.setVisibility(View.VISIBLE);
        } else {
            mImgItem1.setVisibility(View.GONE);
        }
        if (icon2 != 0) {
            mImgItem2.setImageResource(icon2);
            mImgItem2.setVisibility(View.VISIBLE);
        } else {
            mImgItem2.setVisibility(View.GONE);
        }
    }


    public void show() {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 0.7f;
        context.getWindow().setAttributes(lp);
    }

    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        this.setOutsideTouchable(true);
        this.setOnDismissListener(new OnDismissListener() { //退出popupwidon时显示父控件原来的颜色
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow().getAttributes();
                lp.alpha = 1.0f;
                context.getWindow().setAttributes(lp);
            }
        });
    }

    private PopuItemClick mPopuItemClick;

    public void setPopuItemClick(PopuItemClick mPopuItemClick) {
        this.mPopuItemClick = mPopuItemClick;
    }

    @Override
    public void onClick(View v) {
        int type = 0;
        if (v.getId() == R.id.ll_popumenu_1) {
            type = 1;
        } else if (v.getId() == R.id.ll_popumenu_2) {
            type = 2;
        } else if (v.getId() == R.id.tv_popumenu_3) {
            type = 0;
        }
        if (mPopuItemClick != null)
            mPopuItemClick.onItemOclick(v, type);
        dismiss();
    }

    public interface PopuItemClick {
        public void onItemOclick(View view, int type);
    }
}
